<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <div class="app-space-between">
        <div class="app-space-between-left">
          <el-form-item label="评价用户:" prop="creator">
            <el-input v-model="queryParams.creator" placeholder="请输入评价用户" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="评价商品:" prop="postName">
            <el-input v-model="queryParams.postName" placeholder="请输入评价商品" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="评价时间:" prop="orderAssessTime">

            <el-date-picker v-model="queryParams.orderAssessTime" type="daterange" range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" @change="handleQuery">
            </el-date-picker>

          </el-form-item>
        </div>
        <div class="app-space-between-right">
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="评价用户" align="center" prop="creator" />
      <el-table-column label="评价商品" align="center" prop="postCode" />
      <el-table-column label="评价内容" align="center" prop="orderAssessContent" />
      <el-table-column label="评价时间" align="center" prop="orderAssessTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">回复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 回复 -->
    <el-dialog title="回复" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
      <div style="padding-right: 40px;">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="商品名称:">粽子</el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="商品分类:">食品</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规格单位:">袋（6个）600g</el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="回复内容:" prop="orderAssessContent">
                <el-input type="textarea" :rows="5" placeholder="请输入回复的内容" v-model="form.orderAssessContent">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="图片:" prop="orderAssessAddress">
                <image-upload :isShowTip="false" v-model="form.orderAssessAddress" :limit="99"></image-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import {
    assessList,
    assessAdd
  } from "@/api/evaluate/content"
  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        btnLoading: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderAssessTime: '',
          creator: ''
        },
        form: {},
        rules: {
          orderAssessContent: {
            required: true,
            message: "请输入回复内容",
            trigger: "blur"
          }
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.btnLoading = true
            assessAdd(this.form).then(res => {
              this.open = false
              this.btnLoading = false
              this.getList()
            })
          }
        })
      },
      cancel() {
        this.open = false
      },
      handleUpdate(options) {
        this.reset()
        this.form.creatorId = options.creatorId
        this.open = true
      },
      reset() {
        this.form = {
          orderAssessContent: undefined,
          orderAssessAddress: undefined
        };
        this.resetForm("form");
      },
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        let form =  JSON.parse(JSON.stringify(this.queryParams))
        form.startTime = ''
        form.endTime = ''
        if(Array.isArray(form.orderAssessTime)){
          form.startTime =  form.orderAssessTime[0] || ''
          form.endTime =  form.orderAssessTime[1] || ''
        }
        delete form.orderAssessTime

        assessList(form).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      }
    }
  };
</script>
